/*
 * @Author: your name
 * @Date: 2021-07-10 22:14:50
 * @LastEditTime: 2021-08-02 20:43:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \TEST\Core\mycode\openmv.c
 */

#include "openmv.h"
#include"string.h"
#include"float_transmit.h"
int check_if_openmv_data_right(uint8_t *data_buffer,int which);

/**
 * @description: 这个函数用在UART接收中断函数HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)中
 该函数自带串口中断的判断    if(huart == &huart5
 自带串口中断的开启HAL_UART_Receive_IT(&huart5, (uint8_t *)&openmv_a_RxBuffer, 1);
 接收的缓冲区不用再定义，定义在openmv.h中
 所以接收中断回调函数需要#include"openmv.h"
 * @param {*}
 * @return {*}
 */
void receive_openmv_data(UART_HandleTypeDef *huart)
{   
     if(huart == &huart5)
    {
      if(openmv_rx.finished_data == 0||openmv_rx.finished_yuzhi == 0)
      {
        //printf("%c",aRxBuffer[0]);
      if(openmv_rx.counter == 0||openmv_rx.now_data == 1)
      {
    	 openmv_rx.data_buffer[openmv_rx.counter] = openmv_rx.a_buffer[0];
      }

      if(openmv_rx.counter == 0||openmv_rx.now_yuzhi == 1)
      {
       openmv_rx.yuzhi_buffer[openmv_rx.counter] = openmv_rx.a_buffer[0];
      }
        //printf("%c",usart3_jy61.RX_BUF[usart3_jy61.counter]);
        if(openmv_rx.counter==0&&openmv_rx.a_buffer[0]!=0x53)          //�????0号数据不是帧头，跳过
        {
              HAL_UART_Receive_IT(&huart5, (uint8_t *)&openmv_rx.a_buffer, 1);
              return;
        }
      //收到帧头之后首先当前接收的是哪类数据
        if(openmv_rx.counter == 1)
        {
              switch(openmv_rx.a_buffer[0])
              {
              case(0x00):openmv_rx.now_data = 1;break;
              case(0x01):openmv_rx.now_yuzhi = 1;break;
              default:openmv_rx.counter = 0;  HAL_UART_Receive_IT(&huart5, (uint8_t *)&openmv_rx.a_buffer, 1);return;
              }
        }
        openmv_rx.counter = openmv_rx.counter+1;
        //printf("counter = %d\n",usart3_jy61.counter);
       if(openmv_rx.now_data == 1)
      {
        if(openmv_rx.counter==17) //接收�????11个数�????
        {
              if(check_if_openmv_data_right(&openmv_rx.data_buffer[0],0) == 1)
              {
                  openmv_rx.finished_data = 1;
              }
              openmv_rx.counter=0; //重新赋�?�，准备下一帧数据的接收
              openmv_rx.now_data = 0;
        }
      }
      if(openmv_rx.now_yuzhi == 1)
      {
        if(openmv_rx.counter==28) //接收�????11个数�????
        {
              if(check_if_openmv_data_right(&openmv_rx.yuzhi_buffer[0],1) == 1)
              {
                  openmv_rx.finished_yuzhi = 1;
              }
              openmv_rx.counter=0; //重新赋�?�，准备下一帧数据的接收
              openmv_rx.now_yuzhi = 0;
        }
      }
       //printf("finished = %d\n",usart3_jy61.finished);
     }
     
    }
    HAL_UART_Receive_IT(&huart5, (uint8_t *)&openmv_rx.a_buffer, 1);
}



/**
 * @description: 根据缓冲区，判断是否是接收到了一帧的数据
 * @param {uint8_t} *data_buffer
 * @param {int} which
 * @return {*}
 */
int check_if_openmv_data_right(uint8_t *data_buffer,int which)
{
      if(which == 0)
      {
      int a = (*data_buffer == 'S')&&(*(data_buffer+16)=='T');
      return a;
      }
      if(which == 1)
      {
      int b = (*data_buffer == 'S')&&(*(data_buffer+27)=='T');
      return b;
      }
}

/**
 * @description:对openmv缓冲区数据进行处理，主要是将数据拷贝到数据结构体openmv_data中
 * @param {*}
 * @return {*}
 */
void openmv_data_process(void)
{
      if(openmv_rx.finished_data == 1)
      {
            int class_num = openmv_rx.data_buffer[2];
            int target_num = openmv_rx.data_buffer[3];
            char* start =  (char*)(&(openmv_data.class[class_num].target[target_num]));
            memcpy(start,&openmv_rx.data_buffer[4],12);
            openmv_rx.finished_data = 0;
            
      }
      if(openmv_rx.finished_yuzhi == 1)
      {
            int class_num = openmv_rx.yuzhi_buffer[2];
            char* start =  (char*)(&(openmv_yuzhi.class[class_num]));
            memcpy(start,&openmv_rx.yuzhi_buffer[3],24);
            openmv_rx.finished_yuzhi = 0;
      }
}
/**
 * @description: 给openmv发送命令，命令包括3至9注意千万不可快速连续调用
 * @param {int} command
 * @return {*}
 */
void send_command_to_openmv(int command)
{
      strcpy(&(openmv_tx.data_buffer[0]),"S00000T");
      if(command >=3 && command <=9)
      {
            openmv_tx.data_buffer[1] = (char)command+48;
            openmv_tx.data_buffer[7] = '\n';
            HAL_UART_Transmit(&huart5,&(openmv_tx.data_buffer[0]),8,1000);
      }
}

/**
 * @description: 给openmv发送要修改的阈值注意千万不可快速连续调用
 * @param {int} class
 * @param {int} target
 * @param {int} much
 * @return {*}
 */
void send_yuzhi_to_openmv(int class,int target,int much )
{
    strcpy(&(openmv_tx.data_buffer[0]),"S00000T");
    openmv_tx.data_buffer[1] = (char)(class+48);
    openmv_tx.data_buffer[2] = (char)(target+48);
    openmv_tx.data_buffer[3] = (char)(much/100+48);
    openmv_tx.data_buffer[4] = (char)((much/10)%10+48);
    openmv_tx.data_buffer[5] =(char)(much%10+48);
    openmv_tx.data_buffer[7] = '\n';
    HAL_UART_Transmit(&huart5,&(openmv_tx.data_buffer[0]),8,1000);
}


